<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="SearchForm.KeyWord"
        style="width:250px"
        placeholder="可用文字标题查询"
        class="filter-item"
      ></el-input>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查询</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleAdd"
        >添加</el-button
      >
    </div>
    <el-table
      :data="DataList"
      border
      fit
      highlight-current-row
      v-loading="listLoading"
      style="width: 100%"
    >
      <el-table-column prop="Title" label="文章标题" align="center" width="300">
      </el-table-column>
      <el-table-column prop="Abstract" label="文章摘要" align="center">
      </el-table-column>
      <el-table-column prop="Cover" label="封面图" align="center" width="180">
        <template slot-scope="scope">
          <div>
            <img :src="scope.row.Cover" width="75px" height="75px" alt="" />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="CreateTimeStr"
        align="center"
        label="创建时间"
        width="180"
      >
      </el-table-column>
      <el-table-column prop="Tag" align="center" label="标签">
      </el-table-column>
      <el-table-column label="是否开启评论" align="center" width="120">
        <template slot-scope="scope">
          <div>
            {{ scope.row.IsOpenComment ? "已开启" : "未开启" }}
          </div>
        </template>
      </el-table-column>

      <el-table-column label="是否公开" align="center" width="120">
        <template slot-scope="scope">
          <div>
            {{ scope.row.Status==1 ? "已公开" : "未公开" }}
          </div>
        </template>
      </el-table-column>

      <el-table-column align="center" width="250" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.Status == 1"
            type="success"
            size="mini"
            @click="handleShelfUpdate(scope.row, 2)"
            >下架文章</el-button
          >
          <el-button
            v-else
            type="success"
            size="mini"
            @click="handleShelfUpdate(scope.row, 1)"
            >上架文章</el-button
          >
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
            删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="TotalCount > 0"
      :total="TotalCount"
      :page.sync="SearchForm.pageIndex"
      :limit.sync="SearchForm.pageSize"
      @pagination="getList"
    />
    <!-- 编辑新增弹出框 -->
  </div>
</template>

<script>
import req from "@/utils/req";
import Pagination from "@/components/Pagination";
export default {
  components: { Pagination },
  data() {
    return {
      SearchForm: {
        KeyWord: "",
        pageIndex: 1,
        pageSize: 20
      },
      AllTags: [],
      TotalCount: 0,
      DataList: [],
      listLoading: false

      //设置权限菜单的字段
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    handleFilter() {
      this.SearchForm.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      req.get("Blog/Article/pagelist", this.SearchForm).then(res => {
        this.DataList = res.Data.List;
        this.TotalCount = res.Data.TotalCount;
        setTimeout(() => {
          this.listLoading = false;
        }, 500);
      });
    },
    //新增
    handleAdd() {
      this.$router.push({
        path: `/orthers/articleAdd/${0}`
      });
    },
    handleUpdate(row) {
      this.$router.push({
        path: `/orthers/articleAdd/${row.ID}`
      });
    },
    //删除
    handleDelete(row) {
      this.$confirm(`此操作将永久删除该文章 , 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          req.post("blog/Article/Delete", row).then(res => {
            if (res.StatusCode === 200) {
              this.$notify.success({ title: "成功", message: "删除成功" });
              this.DataList = this.DataList.filter(el => {
                return el.ID != row.ID;
              });
            }
          });
        })
        .catch(() => {});
    },
    handleShelfUpdate(row, status) {

      this.$confirm(`是否${status == 2 ? "下架" : "上架"}该文章?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
            row.Status = status;
          this.$req.post("blog/article/Edit", row).then(res => {
            if (res.StatusCode === 200) {
              this.$notify.success({ title: "成功", message: "操作成功" });
              this.getList();
            } else {
              this.$notify.error({ title: "失败", message: res.Data });
            }
          });
        })
        .catch(() => {});
    }
  },
  watch: {}
};
</script>
<style >
.avatar-uploader div {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
